package com.it.service.impl;

import com.it.dao.DeptDao;
import com.it.pojo.Dept;
import com.it.service.DeptService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptDao deptDao;

    @Override
    public boolean insertDept(Dept dept) {
        return deptDao.addDept(dept);
    }

    @HystrixCommand(fallbackMethod = "selectByIdFailed")
    @Override
    public Dept selectById(Long id) {
        Dept dept = deptDao.queryById(id);
        if (dept == null) {
            throw new RuntimeException("id错误");
        }
        return dept;
    }

    public Dept selectByIdFailed(Long id) {
        return new Dept().setId(id).setName("服务器异常, ID => "+id+" 不存在！").setDb_source("数据库异常");
    }


    @Override
    public List<Dept> selectAll() {
        return deptDao.queryAll();
    }
}
